package zj.xinxin.api.moudle.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import zj.xinxin.api.moudle.security.dto.ConsumerDetailDto;

/**
 * @author junzhou
 * @date 2022/3/23 14:03
 * @since 1.8
 */
public class ConsumerUtil {

    /**
     * 获取当前登陆用户信息
     *
     * @return 用户信息
     */
    public static ConsumerDetailDto getUserDetails() {
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        ConsumerDetailDto userDetail = null;
        if (authentication.getPrincipal() instanceof String) {
            userDetail = new ConsumerDetailDto()
                    .setUsername((String) authentication.getPrincipal());
        }
        if (authentication.getPrincipal() instanceof ConsumerDetailDto){
            userDetail = (ConsumerDetailDto) authentication.getPrincipal();
        }
        return userDetail;
    }

}
